
#include "LPC23xx.h"
#include "io.h"
#include "hardware.h"
#include "sim.h"
#include "macros.h"

extern int SIM_CARD;

void SIM_Init ( void )
{
	/* SIM Select */

	PIN_SIM_SELECT_EXTERNAL_DIR = PIN_OUTPUT;
	PIN_SIM_SELECT_A_DIR = PIN_OUTPUT;	
/* 	PIN_SIM_SELECT_B_DIR = PIN_OUTPUT; */
/* 	PIN_SIM_SELECT_C_DIR = PIN_OUTPUT; */
	PIN_SIM_SWITCH_CTRL_DIR = PIN_OUTPUT;
	PIN_SIM_ACTIVATE_DIR = PIN_OUTPUT;


	PIN_SIM_SELECT_EXTERNAL = 1;
	PIN_SIM_SELECT_A = 1;
	PIN_SIM_SELECT_B = 1;
	PIN_SIM_SELECT_C = 1;

	PIN_SIM_SWITCH_CTRL = 1;

 	PIN_SIM_ACTIVATE = 1; 
}

int SIM_Select ( int sim_card )
{
	if ( sim_card != SIM_INTERNAL_A && sim_card != SIM_EXTERNAL )
	{
		return -1;
	} 

	if ( sim_card == SIM_INTERNAL_A )
	{
		PIN_SIM_SWITCH_CTRL = 1;
	}
	else
	{
		PIN_SIM_SWITCH_CTRL = 0; 
	}

	delay_ms(1);
	PIN_SIM_ACTIVATE = 0; 
	delay_ms(50);
	PIN_SIM_ACTIVATE = 1; 
	delay_ms(50);

//	SIM_CARD = sim_card;
	return sim_card;
}
